package com.cr.admin.business.order.controller;

import com.cr.admin.business.order.pojo.entity.Order;
import com.cr.admin.business.order.pojo.param.OrderSaveParam;
import com.cr.admin.business.order.pojo.param.OrderUpdateParam;
import com.cr.admin.business.order.pojo.query.OrderQuery;
import com.cr.admin.business.order.service.OrderService;
import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Api(tags = "95.订单管理")
@RestController
@RequestMapping("/api/bus/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @ApiOperation(value = "查询订单列表")
    @GetMapping
    public JsonResult list(OrderQuery orderQuery) {
        PageData<Order> pageData = PageData.page(orderQuery.getPageNum(), orderQuery.getPageSize(), () -> orderService.list(orderQuery));
        return JsonResult.success(pageData);
    }

    @ApiOperation(value = "新增订单")
    @PostMapping
    public JsonResult insert (@Validated @RequestBody OrderSaveParam orderSaveParam) {
        orderService.saveOrder(orderSaveParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询订单详情")
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        Order order = orderService.selectById(id);
        return JsonResult.success(order);
    }

    @ApiOperation(value = "修改订单")
    @PutMapping
    public JsonResult update(@Validated @RequestBody OrderUpdateParam orderUpdateParam) {
        orderService.updateById(orderUpdateParam);
        return JsonResult.success();
    }
    @ApiOperation(value = "删除订单")
    @DeleteMapping
    public JsonResult delete(@RequestParam(name = "ids") List<Long> ids) {
        orderService.deleteByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询全部订单")
    @GetMapping("/all")
    public JsonResult all() {
        List<Order> orders = orderService.selectList(null);
        return JsonResult.success(orders);
    }
}
